package com.example.financecollegeorderdish.ui

import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.drake.brv.utils.grid
import com.drake.brv.utils.setup
import com.example.financecollegeorderdish.R
import com.example.financecollegeorderdish.databinding.ActivitySelectedBinding
import com.example.financecollegeorderdish.db.bean.DishBean
import com.example.financecollegeorderdish.ui.dialog.LoadingDialog
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.EmptyViewModel
import com.wuhao.lib_base.mvvm.v.BaseActivity
import java.lang.reflect.Type


class SelectedActivity : BaseActivity<ActivitySelectedBinding, EmptyViewModel>() {
    val pmzdbt: Typeface by lazy {
        Typeface.createFromAsset(
            assets,
            "fonts/庞门正道标题体免费版.ttf"
        )
    }//旁门正道标题

    override val mViewModel: EmptyViewModel
        get() = EmptyViewModel()

    override fun createVB(): ActivitySelectedBinding =
        ActivitySelectedBinding.inflate(layoutInflater)

    override fun initObserve() {
    }

    override fun initRequestData() {
    }

    lateinit var dataStr: String
    override fun ActivitySelectedBinding.initView() {
        dataStr = intent.getStringExtra("dataStr")!!//获取用户姓名
        mBinding.appName.typeface = pmzdbt
        mBinding.healthRating.setOnClickListener {

            EvaluateActivity().start(this@SelectedActivity, dataStr)
        }
        mBinding.goOrder.setOnClickListener {
            XPopup.Builder(this@SelectedActivity)
                .dismissOnTouchOutside(false)
                .setPopupCallback(object : SimpleCallback() {
                    override fun onDismiss(popupView: BasePopupView?) {
                        super.onDismiss(popupView)
                        showSuccess("下单成功")
                    }
                })
                .asCustom(LoadingDialog(this@SelectedActivity, 3))
                .show()
        }
        val listType: Type = object : TypeToken<List<DishBean>>() {}.type
        val list = Gson().fromJson<List<DishBean>>(dataStr, listType)
        mBinding.selectedRv.apply {
            grid(4)
            setup {
                addType<DishBean>(R.layout.rv_item_dish)
                onBind {
                    val model = getModel<DishBean>()
                    findView<TextView>(R.id.name).setText(model.name)
                    findView<TextView>(R.id.price).setText("RMB:${model.price}${getString(R.string.price)}")
                    findView<ImageView>(R.id.iv_add).visibility = View.INVISIBLE
                    findView<ImageView>(R.id.iv_minus).visibility = View.INVISIBLE
                    findView<TextView>(R.id.dish_num).setText("${model.selectedNum}")
                    val dishImg = findView<ImageView>(R.id.dish_img)
                    loadImage(model.imagePath, dishImg)
                }
            }.models = list
        }
    }

    fun start(
        context: Context,
        dataStr: String
    ) {
        val starter =
            Intent(context, SelectedActivity::class.java)
        starter.putExtra("dataStr", dataStr)
        context.startActivity(starter)
    }

    /**
     * 加载网络图片
     *
     * @param context   上下文
     * @param url       图片 URL
     * @param imageView 目标 ImageView
     */
    fun loadImage(url: String, imageView: ImageView) {
        //.error(R.mipmap.img_default_xh_target); // 错误图片
        Glide.with(this@SelectedActivity)
            .load(url)
            .into(imageView)
    }
}